require"common"

--==========================判断字符串=========================
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len,1 do
                c = string.sub(num,i,i)
                if tonumber(c) > max or tonumber(c) < 0 then return nil end

        end
        return true
end

--数字字符串排序

function sort_str_pl5( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len do
		table.insert( st, string.sub(num,i,i) )
	end
	table.sort(st)
	for i=1,len do
		out = out..st[i]
	end

	return out

end
--获取字符串中唯一的字符个数

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str_pl5(num)
	for i=1,len do
		c = string.sub(str,i,i)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end
--返回注数,倍投数 , 投注串, 指令串
function random_pl5( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  7#注数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = selectNumber(5,0,1,2,3,4,5,6,7,8,9)
			zlStr = zlStr..nums[1]..nums[2]..nums[3]..nums[4]..nums[5]
			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_pl5('1', zlStr)
	end
	return butNum, butPower, butStr, zlStr
end

--返回注数,倍投数,投注串
function check_pl5( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 51#注码 51#12345#45623 表示自选2注
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 5  then return 0 end
			if  not judge_str(xItem[i],9) then return 0 end
		end
	--复式自选  53#第1位#第2位#第3位#第4位#第5位
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 5  then return 0 end
		for i=1,#xItem  do
			_,distNum = get_distinct_num(xItem[i])
			if distNum ~= string.len(xItem[i]) then return 0 end
			if  not judge_str(xItem[i],9) then return 0 end
		end
		butNum = string.len(xItem[1])*string.len(xItem[2])*string.len(xItem[3])*string.len(xItem[4])*string.len(xItem[5])
	else
		return 0
	end
	return butNum,butPower,inStr,inStr
end
--==============================================================================================================================
function query_pl5(butType,queryStr,djStr)
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local result = 0
	local itemLen = 0;
	itemList = Split( queryStr, "#B" )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	jItem = Split(itemList[1],"#")

	if butType == '1' then
		result = MultSimpCompute(jItem,djStr,butPower,5,1,butType)
	elseif butType == '2' then

		if #jItem ~= 5 then return 0 end
		result = MultSimpCompute(jItem,djStr,butPower,5,1,butType)
	else
		return 0
	end
	return result
end

--print(query_pl5('2',"1#1#3#1#1#B10","11311"))
--print(query_pl5('1',"11311#11311#B10","11311"))
--print(random_pl5('1',"5"))
--print(check_pl5('1',"12345#B10"))
--print(check_pl5('2',"1#2#3#4#5678#B10"))

